<?php  
echo $this->session->flash(); 
if(!empty($payableList)){
    $this->Paginator->options(array(
        'update' => '#payable_listing',
        'evalScripts' => true, 
        //'url' => array('controller' => 'Reports', 'action' => 'checkPayable')
    ));
    //pr($payableList);
} 
?>
<div style="text-align: left; font-weight: bold; padding: 5px 0;"><?php echo $textLabel;?> &nbsp;&nbsp;&nbsp;<?php echo __('MPF'). ' : '.$mpfLabel ?></div>
<div class="file" id="List" style="width:100%; overflow: scroll;">
    <table width="100%" border="0" cellspacing="1" cellpadding="2" class="list">
        <tr class="heading"  bgcolor="#708089">
            <th width="6%" valign="top" align="left"><?php echo __('S.NO') ?></th>
            <th valign="top"  align="left"><?php echo  __('PAYEE', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('PAYEE_ID_PROOF_NO', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('CNT', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('BANK_ACCOUNT_NO', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('JOINING_DATE', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('JOINING_DATE_PLUS_THIRTY', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('MPF_START', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('GROSS_PAYABLE', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('EMPLOYEE_MPF', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('EMPLOYER_MPF', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('NET_PAYABLE', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('MONTH_TO_PAY_MPF', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('CONTRACT', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('CONTRACT_COUNT', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('REPLACEMENT', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('REPLACEMENT_COUNT', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('REIMBURSE', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('REIMBURSE_COUNT', true); ?></th>
            <th valign="top"  align ="left"><?php echo __('GRAND_TOTAL', true); ?></th>
        </tr>
        <?php
        $totalPayable = $totalEmployeeMPF = $totalEmployerMPF = $grandTotalAll = $payment_id_two_count_all = $payment_id_three_count_all = $payment_id_four_count_all = $payment_id_two_contract_count_all = $payment_id_three_contract_count_all = $payment_id_four_contract_count_all = 0;
        if (isset($payableList) && count($payableList) > 0) {
            $i = 0;
            foreach ($payableList as $arrayDetail) {
                ?>
                <tr class="<?php echo (($i%2) == 0)? 'even' : 'odd';?>">
                    <td>&nbsp;<?php echo $i + 1 ?></td>
                    <td><?php
                        echo $this->Js->link(
                                $arrayDetail['Payee']['eng_name'], 
                                array(
                                    'controller' => 'reports',
                                    'action' => 'view_details',$arrayDetail['Payee']['id'], 3
                                ), 
                                array(
                                    'update' => '#detailDiv',
                                    'alt' => __('DETAILS', true),
                                    'title' => __('DETAILS', true),
                                    'escape'=>false,
                                    'before' => $this->Js->get('#detailDiv')->effect('fadeOut'),
                                    'success' => $this->Js->get('#detailDiv')->effect('fadeIn'),
                                    'style' => 'color:#e18f00; font-weight:bold; text-decoration: underline;'
                                )
                            );
                        
                        $employeeMPF = $employerMPF = 0;
                        $mpf_start_date = $join_date_plus_thirty = $month_to_pay_mpf = '';
                        
                        if($arrayDetail['Payable']['total_payable']){
                            $employerMPF = (($arrayDetail['Payable']['total_payable'] * 5) / 100);
                            $employerMPF = ceil($employerMPF);
                            if($arrayDetail['Payable']['total_payable'] > 7100){
                                $employeeMPF = ceil($employerMPF);
                            }
                        }
                        if($arrayDetail['Payee']['joining_date'] != '0000-00-00'){
                            $join_date_plus_thirty = date('M j, Y', strtotime($arrayDetail['Payee']['joining_date'] . " +30 days"));

                            $join_date_plus_thirty_month = date("m", strtotime($join_date_plus_thirty));
                            $join_date_plus_thirty_year = date("Y", strtotime($join_date_plus_thirty));
                            $join_date_plus_thirty_min_date = $join_date_plus_thirty_year.'-'.$join_date_plus_thirty_month.'-1';
                            $mpf_start_date = date('M j, Y', strtotime($join_date_plus_thirty_min_date . " +1 month"));

                            $month_to_pay_mpf = date('M j, Y', strtotime($mpf_start_date . " +1 month"));
                        }
                        if($employerMPF > 1500){
                            $employeeMPF = $employerMPF = 1500;
                        }
                        
                        if(strtotime($month_to_pay_mpf) < strtotime($filterDateMin)){
                            $month_to_pay_mpf = date('M j, Y', strtotime($filterDateMin . " +1 month"));
                        }
                        
                        //----Payment calculation -----
                        $payment_id_two_contract_count = isset($paymentData[$arrayDetail['Payable']['payee_id_proof_no']][2]) ? $paymentData[$arrayDetail['Payable']['payee_id_proof_no']][2]['count'] : 0; 
                        $payment_id_two_count = isset($paymentData[$arrayDetail['Payable']['payee_id_proof_no']][2]) ? $paymentData[$arrayDetail['Payable']['payee_id_proof_no']][2]['total_payable'] : 0; 
                        $payment_id_three_contract_count = isset($paymentData[$arrayDetail['Payable']['payee_id_proof_no']][3]) ? $paymentData[$arrayDetail['Payable']['payee_id_proof_no']][3]['count'] : 0; 
                        $payment_id_three_count = isset($paymentData[$arrayDetail['Payable']['payee_id_proof_no']][3]) ? $paymentData[$arrayDetail['Payable']['payee_id_proof_no']][3]['total_payable'] : 0; 
                        $payment_id_four_contract_count = isset($paymentData[$arrayDetail['Payable']['payee_id_proof_no']][4]) ? $paymentData[$arrayDetail['Payable']['payee_id_proof_no']][4]['count'] : 0; 
                        $payment_id_four_count = isset($paymentData[$arrayDetail['Payable']['payee_id_proof_no']][4]) ? $paymentData[$arrayDetail['Payable']['payee_id_proof_no']][4]['total_payable'] : 0; 
                        
                        $totalPayable = $totalPayable + $arrayDetail['Payable']['total_payable'];
                        $totalEmployeeMPF = $totalEmployeeMPF + $employeeMPF;
                        $totalEmployerMPF = $totalEmployerMPF + $employerMPF;
                        $grandTotal = ($arrayDetail['Payable']['total_payable'] - $employeeMPF + $payment_id_two_count + $payment_id_four_count + $payment_id_four_count);
                        $grandTotalAll = $grandTotalAll + $grandTotal;
                        
                        $payment_id_two_count_all = $payment_id_two_count_all + $payment_id_two_count;
                        $payment_id_three_count_all = $payment_id_three_count_all + $payment_id_three_count;
                        $payment_id_four_count_all = $payment_id_four_count_all + $payment_id_four_count;
                        
                        $payment_id_two_contract_count_all = $payment_id_two_contract_count_all + $payment_id_two_contract_count;
                        $payment_id_three_contract_count_all = $payment_id_three_contract_count_all + $payment_id_three_contract_count;
                        $payment_id_four_contract_count_all = $payment_id_four_contract_count_all + $payment_id_four_contract_count;
                        ?>
                    </td>
                    <td>&nbsp;<?php echo $arrayDetail['Payable']['payee_id_proof_no']; ?></td>
                    <td>&nbsp;<?php echo isset($arrayDetail['Payable']['count']) ? $arrayDetail['Payable']['count'] : '--';?> </td>
                    <td>&nbsp;<?php echo $arrayDetail['Payable']['bank_account_no']; ?></td>
                    <td>&nbsp;<?php echo($arrayDetail['Payee']['joining_date'] != '0000-00-00') ? date('M j, Y', strtotime($arrayDetail['Payee']['joining_date'])) : __('NOT_AVAILABLE'); ?></td>
                    <td>&nbsp;<?php echo $join_date_plus_thirty; ?></td>
                    <td>&nbsp;<?php echo $mpf_start_date; ?></td>
                    <td>&nbsp;<?php echo number_format($arrayDetail['Payable']['total_payable']); ?></td>
                    <td>&nbsp;<?php echo number_format($employeeMPF); ?></td>
                    <td>&nbsp;<?php echo number_format($employerMPF); ?></td>
                    <td>&nbsp;<?php echo number_format(($arrayDetail['Payable']['total_payable'] - $employeeMPF)); ?></td>
                    <td>&nbsp;<?php echo $month_to_pay_mpf; ?></td>
                    <td>&nbsp;<?php echo number_format($payment_id_two_count); ?></td>
                    <td>&nbsp;<?php echo $payment_id_two_contract_count; ?></td>
                    <td>&nbsp;<?php echo number_format($payment_id_three_count); ?></td>
                    <td>&nbsp;<?php echo $payment_id_three_contract_count; ?></td>
                    <td>&nbsp;<?php echo number_format($payment_id_four_count); ?></td>
                    <td>&nbsp;<?php echo $payment_id_four_contract_count; ?></td>
                    <td>&nbsp;<?php echo number_format($grandTotal); ?></td>
                </tr>  
                <?php
                $i++;
            }
        }
        ?>
        <tr class="even">
            <td colspan="8" style="text-align:right;"><strong><?php echo __('PAGE_TOTAL', true);?> : </strong></td>
            <td><?php echo number_format($totalPayable);?></td>
            <td><?php echo number_format(ceil($totalEmployeeMPF)); ?></td>
            <td><?php echo number_format(ceil($totalEmployerMPF)); ?></td>
            <td><?php echo number_format(($totalPayable - $totalEmployeeMPF)); ?></td>
            <td>&nbsp;</td>
            <td><?php echo number_format($payment_id_two_count_all);?></td>
            <td><?php echo $payment_id_two_contract_count_all;?></td>
            <td><?php echo number_format($payment_id_three_count_all);?></td>
            <td><?php echo $payment_id_three_contract_count_all;?></td>
            <td><?php echo number_format($payment_id_four_count_all);?></td>
            <td><?php echo $payment_id_four_contract_count_all;?></td>
            <td><?php echo number_format($grandTotalAll); ?></td>
        </tr>
        <tr>
            <td colspan="12" bgcolor="#ffffff" align="center">
                <div class="pagination">            
                    <?php
                    if (!empty($payableList)) {
                        echo $this->Element('pagination_reports');
                    } else {
                        echo __("NO_RECORD_MESSAGE");
                    }
                    ?>              
                </div>
            </td>
        </tr>
    </table>
</div>

<!-- print section -->
<div style="margin: 10px 0 10px 0;">
    <div style="float: left;">
        <?php echo $this->Form->create("Report", array('action' => 'download_report')); ?>
        <div style="float: right; margin-left: 20px; margin-right: 6px;"><?php 
            echo $this->Form->hidden('Report.year', array('value' => $Year, 'type' => 'textfield'));
            echo $this->Form->hidden('Report.month', array('value' => $Month, 'type' => 'textfield'));
            echo $this->Form->hidden('Report.mpf', array('value' => $mpf, 'type' => 'textfield'));
            echo $this->Form->hidden('Report.switch', array('value' => '2'.$switch, 'type' => 'textfield'));
            echo $this->Form->submit(__('PRINT_TO_PDF', true), array(
                "class" => "button", 
                'label' => false, 
                'div' => false,
                'name' => 'print_pdf'
            ));
            echo '<div class="buttonEnding"></div>';
            ?>
        </div>
        <div style="float: right;"><?php 
            echo $this->Form->submit(__('PRINT_TO_EXCEL', true), array(
                "class" => "button", 
                'label' => false, 
                'div' => false,
                'name' => 'print_excel'
            ));
            echo '<div class="buttonEnding"></div>';?>
        </div>
        <?php echo $this->Form->end(); ?>
        <div style="clear: right;"></div>
    </div>
    <div style="clear: left;"></div>
</div>

<div id="detailDiv" style="margin-top:17px;"></div>
        <!-- Div update ends here -->
<?php echo $this->Js->writeBuffer();?>